package com.minelx.table.core;

import com.minelx.table.core.series.Series;
import com.minelx.table.core.series.source.Source;

import java.util.Arrays;

import static java.util.stream.Collectors.joining;
import static org.junit.jupiter.api.Assertions.assertEquals;

public final class TableTestHelper {

//	@Test
//	static void innerJoin() {
//		Table another = Table.csvText("b,d\n" +
//				"2,3\n" +
//				"2,4\n" +
//				"3,5");
//		assertEquals("a_x,b_x,a_y,c,b_y,d\n" +
//				"1,2,1,4,2,3\n" +
//				"1,2,1,4,2,4\n" +
//				"2,3,2,5,3,5", table.innerJoin(another, "b").toText());
//	}

	static void assertTableEquals(Table table, Table anotherTable) {
		columnIndexes(table, anotherTable);
		rowIndexes(table, anotherTable);
		drop(table, anotherTable);
		column__by_index(table, anotherTable);
		row(table, anotherTable);
		rows(table, anotherTable);
		width(table, anotherTable);
		height(table, anotherTable);
		toText(table, anotherTable);
		column(table, anotherTable);
		title(table, anotherTable);
		head(table, anotherTable);
	}

	static void columnIndexes(Table table, Table anotherTable) {
		assertEquals(
				anotherTable.form().columnIndexes().stream().mapToObj(String::valueOf).collect(joining(",")),
				table.form().columnIndexes().stream().mapToObj(String::valueOf).collect(joining(","))
		);
	}

	static void rowIndexes(Table table, Table anotherTable) {
		assertEquals(
				anotherTable.form().rowIndexes().stream().mapToObj(String::valueOf).collect(joining(",")),
				table.form().rowIndexes().stream().mapToObj(String::valueOf).collect(joining(","))
		);
	}

	static void drop(Table table, Table anotherTable) {
		assertEquals(
				anotherTable.drop(Arrays.asList(table.form().title().at(0), table.form().title().at(1))).toText(),
				table.drop(Arrays.asList(table.form().title().at(0), table.form().title().at(1))).toText()
		);
	}

	static void column__by_index(Table table, Table anotherTable) {
		assertEquals(
				anotherTable.column(table.form().title().at(0)).toText(),
				table.form().column(0).toText()
		);
	}

	static void row(Table table, Table anotherTable) {
		assertEquals(
				anotherTable.form().row(0).toText(),
				table.form().row(0).toText()
		);
	}

	static void rows(Table table, Table anotherTable) {
		assertEquals(
				anotherTable.rows(Series.create(Source.array(new Boolean[]{true, false}))).toText(),
				table.rows(Series.create(Source.array(new Boolean[]{true, false}))).toText()
		);
		assertEquals(
				anotherTable.rows(Series.create(Source.array(new Boolean[]{false, true}))).toText(),
				table.rows(Series.create(Source.array(new Boolean[]{false, true}))).toText()
		);
	}

	static void width(Table table, Table anotherTable) {
		assertEquals(anotherTable.width(), table.width());
	}

	static void height(Table table, Table anotherTable) {
		assertEquals(anotherTable.height(), table.height());
	}

	static void toText(Table table, Table anotherTable) {
		assertEquals(anotherTable.toText(), table.toText());
	}

	static void column(Table table, Table anotherTable) {
		assertEquals(
				anotherTable.column(table.form().title().at(0)).toText(),
				table.column(table.form().title().at(0)).toText()
		);
	}

	static void title(Table table, Table anotherTable) {
		assertEquals(anotherTable.form().title().toText(), table.form().title().toText());
	}

	static void head(Table table, Table anotherTable) {
		// FIXME 2021/8/20  wait for me!!!    test all methods
		assertEquals(anotherTable.head().toText(), table.head().toText());
	}
}
